import type PageDialog from '@/components/page-dialog.vue'
import { ref } from 'vue'

export default function useDialog(editForm?: any) {
  const pageDialogRef = ref<InstanceType<typeof PageDialog>>()
  const formRef = ref()
  const editId = ref(null)

  const showDialog = (row: any) => {
    pageDialogRef.value!.show()
    setTimeout(() => {
      formRef.value.clearValidate()
    }, 0)

    if (!row) {
      editId.value = null
      return
    }
    editId.value = row.id
    for (const key in editForm.value) {
      editForm.value[key] = row[key]
    }
  }

  return {
    editId,
    showDialog,
    formRef,
    pageDialogRef
  }
}
